package com.example.administrator.text.utils;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkInfo;

import org.greenrobot.eventbus.EventBus;


/**
 * Created by liuxiaobin on 2016/12/15
 */

public class NetWorkstateReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
            if(intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)){
                if (android.os.Build.VERSION.SDK_INT  < android.os.Build.VERSION_CODES.LOLLIPOP) {

                    //获得ConnectivityManager对象
                    ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

                    //获取ConnectivityManager对象对应的NetworkInfo对象
                    //获取WIFI连接的信息
                    NetworkInfo wifiNetworkInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
                    //获取移动数据连接的信息
                    NetworkInfo dataNetworkInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
                    NetworkInfo.State state = dataNetworkInfo.getState();
                    boolean isConnected = state == NetworkInfo.State.CONNECTED;// 当然，这边可以更精确的确定状态

                    if (wifiNetworkInfo.isConnected() && dataNetworkInfo.isConnected()) {

                        LogUtil.eE("","..........1.........."+isConnected);
                    } else if (wifiNetworkInfo.isConnected() && !dataNetworkInfo.isConnected()) {
                        LogUtil.eE("","..........2..........");
                    } else if (!wifiNetworkInfo.isConnected() && dataNetworkInfo.isConnected()) {
                        LogUtil.eE("","..........3..........");
                    } else {
                        LogUtil.eE("","..........4..........");
                    }
                } else {
                    //这里的就不写了，前面有写，大同小异
                    System.out.println("API level 大于21");
                    //获得ConnectivityManager对象
                    ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
                    //获取所有网络连接的信息
                    Network[] networks = connMgr.getAllNetworks();
                    //通过循环将网络信息逐个取出来
                    for (int i = 0; i < networks.length; i++) {
                        //获取ConnectivityManager对象对应的NetworkInfo对象
                        NetworkInfo networkInfo = connMgr.getNetworkInfo(networks[i]);
                        NetworkInfo.State state = networkInfo.getState();
                        boolean isConnected = state == NetworkInfo.State.CONNECTED;// 当然，这边可以更精确的确定状态
                        if(networkInfo !=null){
                            if(isConnected){
                                EventBus.getDefault().post("networkChange");
                            }
                            LogUtil.eE("","..........6.........."+isConnected);
                        }
                    }
                }
            }
    }
}
